/**
 * 机器模型
 *
 * Created by snowingsea on 2020/02/24.
 */
const hydra = require('./hydra');

/**
 * 获取机器码
 * @param {Boolean} short 是否获取短码
 * @return {Promise}
 */
function getMachineCode(short = false) {
  return hydra.getMachineCode(short);
}

/**
 * 获取编译配置选项
 * @return {Promise}
 */
function getCompilationOptions() {
  return new Promise((resolve, reject) => {
    hydra.getCompilationOptions()
      .then((compilationOptions) => {
        let compilationOptionsJson;
        try {
          compilationOptionsJson = JSON.parse(compilationOptions);
          const complierConfig = {
            compiler: 'gcc',
            configs: {
              arguments: {},
              macros: {},
              predefined: {},
              properties: {
                'no-builtin': true,
              },
            },
            environment: 'localhost',
            languages: {
              c: {
                standards: [
                  'c89',
                  'c99',
                ],
              },
              'c++': {
                standards: [
                  'c++98',
                  'c++11',
                  'c++14',
                  'c++17',
                ],
              },
            },
          };
          // 天脉
          compilationOptionsJson['project types']['gcc-ACoreOS'] = { ...complierConfig };
          // 翼辉
          compilationOptionsJson['project types']['gcc-SylixOS'] = { ...complierConfig };

          resolve(compilationOptionsJson);
        } catch (e) {
          reject(new Error(`Fail to parse message content to JSON: ${compilationOptions}`));
        }
      })
      .catch(reject);
  });
}

/**
 * Export model definition object.
 */
module.exports = {
  getCode: getMachineCode,
  getCompilationOptions,
};
